package org.study.minio.boot.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.study.minio.boot.service.MinioService;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2023年12月12日
 */
@Slf4j
@RequiredArgsConstructor
@CrossOrigin
@RestController
@RequestMapping(value = "/video")
public class VideoController {
    private final MinioService minioService;

    @RequestMapping("/1")
    public ModelAndView toVideo1() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("video1");
        return modelAndView;
    }

    @RequestMapping("/11")
    public ModelAndView toVideo11() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("video1_1");
        return modelAndView;
    }

    @RequestMapping("/12")
    public ModelAndView toVideo12() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("video1_2");
        return modelAndView;
    }

    @RequestMapping("/13")
    public ModelAndView toVideo13() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("video1_3");
        return modelAndView;
    }

    @RequestMapping("/14")
    public ModelAndView toVideo14() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("video1_4");
        return modelAndView;
    }

    @RequestMapping("/2")
    public ModelAndView toVideo2() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("video2");
        return modelAndView;
    }

    @RequestMapping(value = "/3/{bucketName}/{objectName}")
    public ModelAndView toVideo3(@PathVariable(value = "bucketName") String bucketName,
                                 @PathVariable(value = "objectName") String objectName) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("bucketName", bucketName);
        modelAndView.addObject("objectName", objectName);
        modelAndView.setViewName("video3");
        return modelAndView;
    }

    /**
     * 支持分段读取视频流
     *
     * @param request    请求对象
     * @param response   响应对象
     * @param bucketName 视频所在桶的位置
     * @param objectName 视频的文件名
     */
    @GetMapping(value = "/play1/{bucketName}/{objectName}")
    public void playVideo(HttpServletRequest request, HttpServletResponse response,
                          @PathVariable(value = "bucketName") String bucketName,
                          @PathVariable(value = "objectName") String objectName) throws Exception {
        minioService.videoPlay1(request, response, bucketName, objectName);
    }

    @GetMapping(value = "/play2/{bucketName}/{objectName}")
    public void downloadSlice(HttpServletRequest request, HttpServletResponse response,
                              @PathVariable(value = "bucketName") String bucketName,
                              @PathVariable(value = "objectName") String objectName) throws Exception {
        minioService.videoPlay2(request, response, bucketName, objectName);
    }
}
